package com.huntero.hweather;

import com.huntero.hweather.api.IWeather;
import com.huntero.hweather.api.IWeather.IWeatherEventHandler;
import com.huntero.hweather.api.SWA;
import com.huntero.hweather.bean.BDWeather;
import com.huntero.hweather.bean.Weather;
import com.huntero.hweather.utils.HttpHelper;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WeatherWidget extends AppWidgetProvider {

	public static final String REFRESH = "com.huntero.hweather.action.Refresh";

	private WeatherEventHandler mEventHandler = null;
	private static IWeather mWeather=null;
	/**
	 * 一般不需要重写
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("Huntero", "Widget > onReceive action=" + intent.getAction());
		super.onReceive(context, intent);
		if (REFRESH.equals(intent.getAction())) {
			mEventHandler = new WeatherEventHandler(context);
			if(mWeather == null){
				mWeather = new SWA(mEventHandler);
			}
			mWeather.dataAsync("101090101", IWeather.WEATHER_TYPE_FORECAST3D);
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<appWidgetIds.length;i++){
			sb.append(appWidgetIds[i]+" ");
		}
		
		Log.i("Huntero", "Widget > onUpdate");
		
		mEventHandler = new WeatherEventHandler(context);
		if(mWeather == null){
			mWeather = new SWA(mEventHandler);
		}
		mWeather.dataAsync("101090101", IWeather.WEATHER_TYPE_FORECAST3D);
		
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
	}
	private class WeatherEventHandler implements IWeatherEventHandler{
		private Context mContext;
		public int[] appWidgetIds;
		final AppWidgetManager awm;
		public WeatherEventHandler(Context context){
			this.mContext = context;
			this.awm = AppWidgetManager.getInstance(this.mContext);
			this.appWidgetIds = this.awm.getAppWidgetIds(new ComponentName(this.mContext, WeatherWidget.class.getName()));
		}
		@Override
		public void onPreStart() {
			Toast.makeText(this.mContext, "更新 ...", Toast.LENGTH_SHORT).show();
		}
		
		@Override
		public void onFinished(Object data) {
			if(data == null){
				Toast.makeText(this.mContext, "是不是忘记打开网络了", Toast.LENGTH_SHORT).show();
			}else{
				String str = (String) data;
				Weather weather = new Weather(str);
				Log.i("Huntero",weather.toString());
				final String pkgName=this.mContext.getPackageName();
				final int layoutId=R.layout.widget_weather;
				RemoteViews rv = null;
				for (int i = 0; i < appWidgetIds.length; i++) {
					Log.i("Huntero", "> update id="+appWidgetIds[i]);
					rv = new RemoteViews(pkgName,layoutId);
					_initView(rv,weather);
					awm.updateAppWidget(appWidgetIds[i], rv);
				}
				
				Toast.makeText(this.mContext, "完成", Toast.LENGTH_SHORT).show();
			}
		}
		private void _initView(RemoteViews view,Weather w){
			view.setTextViewText(R.id.weather_temp, w.infos[0].dayTemp+"~"+w.infos[0].nightTemp);
			view.setTextViewText(R.id.weather_addr, w.cityName);
			Uri img = w.infos[0].getWeatherImageUri();
			view.setImageViewUri(R.id.weather_img, img);
			view.setTextViewText(R.id.weather_day, "晴天");
			view.setTextViewText(R.id.weather_shidu_value, "%");
			view.setTextViewText(R.id.weather_time," ("+w.getPublishTime()+")" );
			view.setTextViewText(R.id.weather_wind_direct, " ");
			view.setTextViewText(R.id.weather_wind_strong," ");
			
			PendingIntent pendingIntent = PendingIntent.getBroadcast(
					this.mContext, 1, new Intent(REFRESH), 0);
			view.setOnClickPendingIntent(R.id.btn_refresh, pendingIntent);
		}
	};

}
